<#assign tit>租箱租用</#assign>
<@page.html title=tit>
<script type="text/javascript" src="${_appPath!}/js/cal.js"></script>
<script type="text/javascript">
  var _cinfo = null;
  function getmb(c) {
    if(c!="") {
      getJson("getMember?cardCode="+c, function(data){
        if(data.ret) {
          var mc = data.data;
          var fm = getEl("fm_input");
          fm.cardCode.value = mc.card;
          fm.memberCode.value = mc.code;
          fm.memberName.value = mc.name;
          fm.earnestId.value = "";
          fm.earnestAmount.value = "";
          _cinfo = mc;
          chgstm();
        } else {
          window.alert(data.msg);
          clearData();
        }
      });
    }
  }

  function clearData() {
    var fm = getEl("fm_input");
    fm.memberCode.value = "";
    fm.memberName.value = "";
    fm.earnestId.value = "";
    fm.earnestAmount.value = "";
    _cinfo = null;
  }

  function seldj() {
    var fm = getEl("fm_input");
    var url = "getEarnestList?type=5";
    if(fm.memberCode.value=="") {
      window.alert("请先输入会员卡号");
      return;
    }
    url += "&memberCode="+fm.memberCode.value;
    openWin(url, 620, 400);
  }
  function seldjOk(vv) {
    var ss = vv.split(",");
    var fm = getEl("fm_input");
    fm.earnestId.value = ss[0];
    fm.earnestAmount.value = ss[1];
    calcpay();
  }

  function chgstm() {
    var fm = getEl("fm_input");
    var m = parseInt(fm.rentMonth.value,10);
    if(isNaN(m) || m<1) {
      m = 1;
      fm.rentMonth.value = m;
    }
    var dt = parseDate(fm.beginTime.value);
    if(dt==null) {
      dt = new Date();
      fm.beginTime.value = getDateStr(dt);
    }
    dt.addMonth(m);
    fm.endTime.value = getDateStr(dt);

    calcr();
  }

  function calcr() {
    var fm = getEl("fm_input");
    var m = parseInt(fm.rentMonth.value,10);
    if(isNaN(m) || m<1) {
      return;
    }
    var yy = parseInt(m/12);
    var mm = m%12;
    var money = yy*${box.yearFee!}+mm*${box.monthFee!};
    fm.rentAmount.value = formatMoney(money);
  }
  function calcpay() {
    var fm = getEl("fm_input");
    if(fm.payAmount.value=="") return;
    var m1 = parseFloat(fm.payAmount.value);
    if(isNaN(m1)) return;
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m3 = 0;
    if(fm.depositAmount.value!="") m3 = parseFloat(fm.depositAmount.value);
    fm.factAmount.value = formatMoney(m1+m2-m3);
    fm.payAmount.value = formatMoney(m1);
  }

  function chgfact() {
    var fm = getEl("fm_input");
    if(fm.factAmount.value=="") {
      fm.payAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.factAmount.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m3 = 0;
    if(fm.depositAmount.value!="") m3 = parseFloat(fm.depositAmount.value);
    fm.factAmount.value = formatMoney(m1);
    fm.payAmount.value = formatMoney(m1+m3-m2);
  }

  function chgpaym() {
    var fm = getEl("fm_input");
    if(fm.payAmount.value=="") {
      fm.factAmount.value = "";
      return;
    }
    var m1 = parseFloat(fm.payAmount.value);
    if(isNaN(m1)) {
      window.alert("金额错误");
      return;
    }
    var m2 = 0;
    if(fm.earnestAmount.value!="") m2 = parseFloat(fm.earnestAmount.value);
    var m3 = 0;
    if(fm.depositAmount.value!="") m3 = parseFloat(fm.depositAmount.value);
    fm.factAmount.value = formatMoney(m1+m2-m3);
    fm.payAmount.value = formatMoney(m1);
  }

  function save(fm) {
    if(!fm) fm = getEl("fm_input");
    if(!_cinfo) {
      window.alert("请输入会员卡号");
      return;
    }

    var mm = fm.payAmount.value;
    post(fm, function(){
      openWin("getPay?money="+mm, 500, 400);
      return false;
    });
    return false;
  }
  function payOk(vv) {
    var fm = getEl("fm_input");
    fm.payls.value = vv;
    postForm(fm.action, fm, saveRet);
  }
  function saveRet(data) {
    window.alert(data.msg);
    if(data.ret) cload();
  }
</script>
<div style="width:600px;">

  <form id="fm_input" method="post" action="_add" onsubmit="return save(this);">
    <input type="hidden" name="payls" value="">
    <div class="kbox">
      <table class="input w4">
        <tr>
          <th width="85">会员卡号</th>
          <td width="160"><input type="text" name="cardCode" value="" onchange="getmb(this.value);" maxlength="20"></td>
          <th width="85"></th>
          <td width="160"><!--label><input type="checkbox" class="cb" name="signStatus" value="0">不使用手写签名</label--></td>
        </tr>
        <tr>
          <th>会员姓名</th>
          <td><input type="text" name="memberName" value="" maxlength="10" class="disable" readonly></td>
          <th>会员编号</th>
          <td><input type="text" name="memberCode" value="" class="disable" readonly></td>
        </tr>
        <tr>
          <th>定金编号</th>
          <td><input type="text" name="earnestId" value="" style="width:90px;" class="disable" readonly>
            <input type="button" class="opwin" value="..." onclick="seldj();"/></td>
          <th>定金金额</th>
          <td><input type="text" name="earnestAmount" value="" class="disable" readonly></td>
        </tr>

        <tr>
          <th>租箱号</th>
          <td><input type="text" name="boxCode" value="${box.code!}" class="disable" readonly></td>
          <th>租用月数</th>
          <td><input type="text" name="rentMonth" value="" onchange="chgstm();"></td>
        </tr>
        <tr>
          <th>开始日期</th>
          <td><input type="text" name="beginTime" value="" onchange="chgstm();" class="date" maxlength="10" onfocus="cal(this);"></td>
          <th>结束日期</th>
          <td><input type="text" name="endTime" value="" class="date disable" maxlength="10" readonly></td>
        </tr>
        <tr>
          <th>应收租金</th>
          <td><input type="text" name="rentAmount" value="" class="disable" readonly></td>
          <th>实收租金</th>
          <td><input type="text" name="factAmount" value="" onchange="chgfact();" req="实收租金"></td>
        </tr>
        <tr>
          <th>押金金额</th>
          <td><input type="text" name="depositAmount" value="${box.deposit?string("0.00")}" class="disable" readonly></td>
          <th>应收金额</th>
          <td><input type="text" name="payAmount" value="" onchange="chgpaym();" req="应收金额"></td>
        </tr>
      </table>
    </div>
    <div class="button"><input type="button" value="确认并提交" onclick="save();"></div>
  </form>
</div>

</@page.html>